﻿using System.Linq.Expressions;
using Medical.Infrastructrue;
using Medical.Infrastructrue.Interfaces;
using Microsoft.EntityFrameworkCore;

namespace BaseData.Infrastructrue.Inplements
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private readonly Mycontext context;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_context"> 数据库上下文 </param>
        public BaseRepository(Mycontext _context)
        {
            context = _context;
        }
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public Mycontext DB { get { return context; } }
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetAll()
        {
            return context.Set<T>().AsQueryable();
        }
        /// <summary>
        /// 根据表达式获取单条数据
        /// </summary>
        /// <param name="expression"> 表达式 </param>
        /// <returns></returns>
        public async Task<T> GetModel(Expression<Func<T,bool>> expression)
        {
            return await context.Set<T>().FirstOrDefaultAsync(expression);
        } 
        /// <summary>
        /// 异步插入数据
        /// </summary>
        /// <param name="entity"> 实体 </param>
        /// <returns> 受影响的行数 </returns> 
        public async Task<int> InsertAsync(T entity)
        {
            await context.Set<T>().AddAsync(entity);
            return await context.SaveChangesAsync();
        }
        /// <summary>
        /// 异步更新数据
        /// </summary>
        /// <param name="entity"> 实体 </param>
        /// <returns> 受影响的行数 </returns>   
        public async Task<int> UpdateAsync(T entity)
        {
            context.Set<T>().Update(entity);
            return await context.SaveChangesAsync();
        }
        /// <summary>
        /// 根据Id异步获取数据
        /// </summary>
        /// <param name="Id"> </param>
        /// <returns></returns>
        public async Task<T> GetByIdAsync(int Id)
        {
            return await context.Set<T>().FindAsync(Id);
        }
        public async Task<T> GetByConditionAsync(Func<object, object> value)
        {
                       //这里的value是一个表达式树，需要根据实际情况进行转换和使用
            var parameter = Expression.Parameter(typeof(T), "x");
            var body = (Expression)value(parameter);
            var lambda = Expression.Lambda<Func<T, bool>>(body, parameter);
            return await context.Set<T>().FirstOrDefaultAsync(lambda);
        }
        public async Task<int> AddRangeAsync(List<T> list)
        {
            await context.Set<T>().AddRangeAsync(list);
            return await context.SaveChangesAsync();
        }
        public Task<int> DeleteRangeAsync(List<T> list)
        {
            context.Set<T>().RemoveRange(list);
            return context.SaveChangesAsync();
        }
        public Task<int> UpdateRangeAsync(List<T> list)
        {
            foreach (var entity in list)
            {
                var entry = context.Entry(entity);
                if (entry.State == EntityState.Detached)
                {
                    context.Set<T>().Attach(entity);
                }
                entry.State = EntityState.Modified;
            }
            return context.SaveChangesAsync();
        }
    }
}
